﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using InsideDL.MISData;
using MISInterface;

namespace InsideDL
{
    public partial class frmAccInput : MISControl.frmMatrix //Form
    {
        protected object[] ftpPara = null;
        protected MISService.VerifyMatrix verifyObj = null;
        protected string uploadObj = string.Empty;
        public frmAccInput()
        {
            InitializeComponent();
            ucExecute_Exit.ExecuteClick += new EventHandler(ucClickFacade);
            ucExecAC001.ExecuteClick += new EventHandler(AC001_Click);
            ucExeARTM973.ExecuteClick += new EventHandler(ARTM973_Click);
            init();
        }

        protected void init()
        {
            Utility.LoadConfigValue("FTPCollection");
            setProperty();            
        }

        protected void setProperty()
        {
            ftp = Utility.GetConfigValue("ftp");
            id = Utility.GetConfigValue("ftpid");
            pw = Utility.GetConfigValue("ftppw");
            currentYM = Utility.GetCurrentYM();
            //ftpPara = new object[] {ftp,id,pw,currentYM};
        }

        protected override void execute()
        {
            if (!checkFileInFTPSite())
            {
                showMessage(this.Text, "請確認FTP檔案");
                return;
            }
            /* 改由別人寄檔案來，程式由暫存路徑取得檔案
            if (!Utility.DownloadFile(ftp, id, pw, downList, Program.TmpPath()))
            {
                //ShowStatus("取得FTP檔案");
                return;
            }
            */
            DataInside di = new DataInside();
            string ymd = string.Empty;
            if (downList.Length > 1)
            {
                Program.CreateFile(Program.TmpPath() + "\\schema.ini",
                    Utility.GetARTMABCDESchema(downList, di.GetCSVFields(
                    new[] { downList[0].ToString(), downList[2].ToString()
                    , downList[3].ToString() }
                    , Program.TmpPath())));


                ymd = Utility.GetLastDate(downList[0]);            
            }
            
            iUpload upload = System.Activator.CreateInstance(
                Type.GetType(uploadObj)) as iUpload;

            object[] para = new object[] {Program.TmpPath(), downList,ymd
            ,Utility.GetThisLastMonth(downList[0])
            ,Utility.GetCurrentDay(downList[0])
            };
            
            upload.Upload(para);
            showMessage(this.Text, "完成！");
                        
        }

        protected void ARTM973_Click(object sender, EventArgs e)
        {
            verifyObj = new InsideDL.MISService.VerifyARTM95_ABCDE();
            uploadObj = "InsideDL.MISData.DataARTM95_ABCDE";
            //backgroundWorker1.RunWorkerAsync();
            execute();                        
        }

        protected void AC001_Click(object sender, EventArgs e)
        {
            verifyObj = new InsideDL.MISService.VerifyAC001();
            uploadObj = "InsideDL.MISData.DataAC001";
            execute();
        }

        protected bool checkFileInFTPSite()
        {   
            /*         
            ftpPara = new object[]{ Utility.GetFTPFileList(ftp, id, pw) ,
            Utility.GetCurrentYM(newDateTime)};
            */
            ftpPara = new object[]{ Utility.GetFileList(Program.TmpPath()) ,
            Utility.GetCurrentYM(newDateTime)};
            bool ok = verifyObj.CheckFileInFtp(ftpPara);
            if (ok == true)
            {
                downList = verifyObj.GetDownList();
            }                                    
            return ok;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            execute();
            //InsideDL.MISData.DataARTM973 artm =
            //    new InsideDL.MISData.DataARTM973(string.Format("{0}\\{1}", Program.TmpPath(), downList[0]));
            //progressBar1.Maximum = artm.Max;
            
            //artm.backgroundWorker1 = backgroundWorker1;
            //artm.Upload();
            
        }

        private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            showMessage(this.Text, "完成");
        }

        private void ucExeARTM973_Load(object sender, EventArgs e)
        {

        }
    }
}
